package org.zjt.platform.demo.service.imp;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.zjt.platform.demo.repository.TestDao;
import org.zjt.platform.demo.entity.TestPOJO;
import org.zjt.platform.demo.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;


@Service
@Slf4j
public class TestServiceImp implements ITestService{

    @Autowired
    private TestDao testDao;



    @Override
    public List<TestPOJO> get(Long age) {
        return testDao.get(age);
    }

    @Transactional
    @Override
    public void updateUser(TestPOJO pojo){
        testDao.update(pojo);
    }



    AtomicInteger num = new AtomicInteger(0);

    @Transactional(isolation= Isolation.READ_UNCOMMITTED)
    @Override
    public void concurrentUpdata(TestPOJO pojo)throws Exception{
        TestPOJO pojo1 = testDao.get(pojo.getId()).get(0);
        log.info(JSONObject.toJSONString(pojo1));
        pojo1.setAge(pojo1.getAge()+1);
        log.info(String.valueOf(num.incrementAndGet()));
        testDao.update(pojo1); //131
    }

}
